<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
// import echarts from "echarts";
// require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";
const hours = [
  "00:00-04:00",
  "04:00-08:00",
  "08:00-12:00",
  "12:00-16:00",
  "16:00-20:00",
  "20:00-24:00",
];
const months = ["1月1", "1月2", "1月3", "1月4", "1月5", "1月6", "1月7"];
// const months = [
//   "2023-01-26",
//   "2023-01-27",
//   "2023-01-28",
//   "2023-01-29",
//   "2023-01-30",
//   "2023-01-31",
//   "2023-02-01",
// ];

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "300px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Array,
      default: [],
      required: true,
    },
    xdata: {
      type: Array,
      default: [],
    },
    titleText: {
      type: String,
      default: "审批耗时",
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    xdata: {
      deep: true,
      handler(val) {
        console.log("xData:", val, "chart:", this.chartData);
        this.setOptions();
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(this.$el, "macarons");
      this.setOptions();
    },
    setOptions() {
      this.chart.setOption({
        title: [
          {
            text: "{a|}",
            left: "5%",
            textStyle: {
              rich: {
                a: {
                  height: 16,
                  width: 4,
                  lineHight: 16,
                  backgroundColor: "#567BCB",
                },
              },
            },
          },
          {
            text: this.titleText,
            left: "7%",
            textStyle: {
              fontSize: 16,
              width: 100,
              color: "#454c5c",
              fontWeight: 500,
              lineHeight: 20,
            },
          },
        ],
        tooltip: {
          position: "top",
        },
        xAxis: {
          type: "category",
          data: this.xdata,
          splitArea: {
            show: true,
          },
          axisTick: {
            show: false,
          },
          axisLine: {
            show: false,
          },
          axisLabel: {
            textStyle: {
              fontWeight: 400,
              color: "#999999",
            },
          },
        },
        yAxis: {
          type: "category",
          data: hours,
          splitArea: {
            show: true,
          },
          axisTick: {
            show: false,
          },
          axisLine: {
            show: false,
          },
          axisLabel: {
            textStyle: {
              fontWeight: 400,
              color: "#999999",
            },
          },
        },
        visualMap: {
          show: false,
          min: 0,
          max: 10,
          calculable: true,
          orient: "horizontal",
          left: "center",
          bottom: "2%",
          inRange: {
            color: ["#F6F8FE", "#6D92F1"], // 修改热力图的颜色 淡蓝色=>深蓝色的过度
          },
        },
        series: [
          {
            name: "审核的任务",
            type: "heatmap",
            data: this.chartData,
            label: {
              show: true,
            },
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      });
    },
  },
};
</script>
